package com.wantupai.mvp.framework.support.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;


import com.wantupai.mvp.framework.support.MvpDelegateCallback;
import com.wantupai.mvp.framework.support.ProxyMvpDelegateCallBack;
import com.wantupai.mvp.framework.support.manager.PresenterManager;
import com.wantupai.mvp.mvp.MvpPresenter;
import com.wantupai.mvp.mvp.MvpView;

import java.util.UUID;


/**
 * 代理模式->第一重代理->生命周期->目标对象：
 * 实现生命周期方法(很多功能，例如：绑定和解绑、数据缓存、
 * View状态缓存等等..一些逻辑)
 *
 * @param <V>
 * @param <P>
 * @author :zhangqing
 */
public class ActivityMvpDelegateImpl<V extends MvpView, P extends MvpPresenter<V>> implements ActivityMvpDelegate<V, P>
{

    private static final String KEY_VIEW_ID = "cn.doolii.mvp.framework.support.id";
    protected ProxyMvpDelegateCallBack<V, P> mProxyMvpCallBack;
    protected boolean keepPresenterInstance;
    protected Activity mActivity;
    protected String viewId;
    private View view;

    public ActivityMvpDelegateImpl(Activity activity, MvpDelegateCallback<V, P> mvpCallback, boolean keepPresenterInstance)
    {
        mProxyMvpCallBack = new ProxyMvpDelegateCallBack<V, P>(mvpCallback);
        mActivity = activity;
        this.keepPresenterInstance = keepPresenterInstance;
    }

    private P getPresenter()
    {
        P presenter = mProxyMvpCallBack.getPresenter();
        if (presenter == null)
        {
            presenter = mProxyMvpCallBack.createPresenter();
            if (presenter == null)
            {
                throw new NullPointerException("presenter不能为空!!!");
            }
        }
        return presenter;
    }

    private V getMvpView()
    {
        V view = mProxyMvpCallBack.getMvpView();
        if (view == null)
        {
            throw new NullPointerException("view不能给为空!!!");
        }
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        P presenter = null;
        if (savedInstanceState != null && keepPresenterInstance)
        {
            viewId = savedInstanceState.getString(KEY_VIEW_ID);
            //说明不存在
            if (viewId == null)
            {
                presenter = createViewIdAndCreatePresenter();
            } //否则不处理
        }
        else
        {
            //直接创建->说明没有缓存
            presenter = createViewIdAndCreatePresenter();
        }
        //绑定P
        mProxyMvpCallBack.setPresenter(presenter);
        getPresenter().attach(getMvpView());
        getPresenter().onCreate(savedInstanceState);
    }

    @Override
    public void onStart()
    {
        getPresenter().onStart();
    }

    @Override
    public void onRestart()
    {
        getPresenter().onRestart();
    }

    @Override
    public void onResume()
    {
        getPresenter().onResume();
    }

    @Override
    public void onPause()
    {
        getPresenter().onPause();
    }

    @Override
    public void onStop()
    {
        getPresenter().onStop();
    }

    //是否存在这个实例(不存在要销毁->返回false，存在返回true不需要销毁)
    public static boolean retainPresenterInstance(boolean keepPresenterInstance, Activity activity)
    {
        return keepPresenterInstance && (activity.isChangingConfigurations() || !activity.isFinishing());
    }

    @Override
    public void onDestroy()
    {
        boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, mActivity);
        getPresenter().destory();
        getPresenter().detach();
        //同时P层也需要销毁
        if (!retainPresenterInstance)
        {
            getPresenter().destory();
        }

        if (!retainPresenterInstance && viewId != null)
        {
            PresenterManager.remove(mActivity, viewId);
        }
    }


    private P createViewIdAndCreatePresenter()
    {
        //缓存P
        P presenter = mProxyMvpCallBack.createPresenter();
        if (presenter == null)
        {
            //如果为空，说明客户端实现类没有创建P对象
            throw new NullPointerException("presenter不能为空!!!");
        }
        if (keepPresenterInstance)
        {
            //第一次进来都会创建
            viewId = UUID.randomUUID().toString();
            //缓存->PresenterManager
            PresenterManager.putPresenter(mActivity, viewId, presenter);
        }
        return presenter;
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {

    }
    @Override
    public void onPostCreate(@Nullable Bundle savedInstanceState)
    {

    }
}
